package com.seckill.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Levi
 * @version 1.0
 * @datetime 2021/9/4 21:39
 * @decription rabbitmq配置类
 **/
@Configuration
public class RabbitMQConfig {

    /**
     * fanout广播模式
     */
    private static final String QUEUE01 = "queue-fanout01";
    private static final String QUEUE02 = "queue-fanout02";
    private static final String EXCHANGE = "fanoutExchange";

    /**
     * direct路由键模式
     */
    private static final String QUEUE03 = "queue-direct01";
    private static final String QUEUE04 = "queue-direct02";
    private static final String DIRECTEXCHANGE = "directExchange";
    private static final String ROUTINGKEY01 = "queue.red";
    private static final String ROUTINGKEY02 = "queue.green";

    /**
     * topic模式
     * @return
     */
    private static final String QUEUE05 = "queue-topic01";
    private static final String QUEUE06 = "queue-topic02";
    private static final String TOPICEXCHANGE = "topicExchange";
    private static final String ROUTINGKEY03 = "#.queue.#";
    private static final String ROUTINGKEY04 = "*.queue.#";

    /**
     * headers模式
     * @return
     */
    private static final String QUEUE07 = "queue-header01";
    private static final String QUEUE08 = "queue-header02";
    private static final String HEADEREXCHANGE = "headerExchange";

    @Bean
    public Queue queue() {
        return new Queue("queue", true);
    }

    @Bean
    public Queue queue01() {
        return new Queue(QUEUE01);
    }

    @Bean
    public Queue queue02() {
        return new Queue(QUEUE02);
    }

    @Bean
    public FanoutExchange fanoutExchange() {
        return new FanoutExchange(EXCHANGE);
    }

    @Bean
    public Binding binding01(){
        return BindingBuilder.bind(queue01()).to(fanoutExchange());
    }

    @Bean
    public Binding binding02(){
        return BindingBuilder.bind(queue02()).to(fanoutExchange());
    }

    @Bean
    public Queue queue03() {
        return new Queue(QUEUE03);
    }

    @Bean
    public Queue queue04() {
        return new Queue(QUEUE04);
    }

    @Bean
    public DirectExchange directExchange(){
        return new DirectExchange(DIRECTEXCHANGE);
    }

    @Bean
    public Binding binding03(){
        return BindingBuilder.bind(queue03()).to(directExchange()).with(ROUTINGKEY01);
    }

    @Bean
    public Binding binding04(){
        return BindingBuilder.bind(queue04()).to(directExchange()).with(ROUTINGKEY02);
    }

    @Bean
    public Queue queue05() {
        return new Queue(QUEUE05);
    }

    @Bean
    public Queue queue06() {
        return new Queue(QUEUE06);
    }

    @Bean
    public TopicExchange topicExchange(){
        return new TopicExchange(TOPICEXCHANGE);
    }

    @Bean
    public Binding binding05(){
        return BindingBuilder.bind(queue05()).to(topicExchange()).with(ROUTINGKEY03);
    }

    @Bean
    public Binding binding06(){
        return BindingBuilder.bind(queue06()).to(topicExchange()).with(ROUTINGKEY04);
    }

    @Bean
    public Queue queue07() {
        return new Queue(QUEUE07);
    }

    @Bean
    public Queue queue08() {
        return new Queue(QUEUE08);
    }

    @Bean
    public Binding binding07(){
        Map<String,Object> map = new HashMap<>();
        map.put("color","red");
        map.put("speed","low");
        return BindingBuilder.bind(queue07()).to(headersExchange()).whereAny(map).match();
    }

    @Bean
    public Binding binding08(){
        Map<String,Object> map = new HashMap<>();
        map.put("color","red");
        map.put("speed","fast");
        return BindingBuilder.bind(queue08()).to(headersExchange()).whereAll(map).match();
    }

    @Bean
    public HeadersExchange headersExchange(){
        return new HeadersExchange(HEADEREXCHANGE);
    }

}
